﻿using System;
using System.IO;
using System.Xml;
using An_Im.Xml;
using System.Linq;

namespace An_Im
{
    // 此处用于实现对侧边任务窗格的配置。
    internal static partial class TaskPaneManager
    {
        internal static int _anStormPaneWidth = 200; // 动画编辑部分
        internal static int _anRecorderPaneWidth = 200; // 动画录制播放
        internal static int _anMacroPaneWidth = 200; // 宏管理部分
        internal static int _anCommandPaneWidth = 200; // 动画命令部分
        internal static int _anSliderPaneWidth = 200; // 滑块控制
        public static void LoadTaskPaneConfig()
        {
            var path = CoreManipulator.TaskPaneConfig;
            if (File.Exists(path))
            {
                var xmldoc = new XmlDocument();
                xmldoc.Load(path);
                var root = xmldoc.DocumentElement;
                _anStormPaneWidth = root["StormPaneWidth"].InnerText.ToInt(200);
                _anRecorderPaneWidth = root["RecorderPaneWidth"].InnerText.ToInt(200);
                _anMacroPaneWidth = root["MacroPaneWidth"].InnerText.ToInt(200);
                _anCommandPaneWidth = root["CommandPaneWidth"].InnerText.ToInt(200);
            }
        }

        /// <summary>
        /// 更新任务栏状态。
        /// </summary>
        public static void UpdateCurrentTaskPaneState()
        {
            if (taskPaneDic.Count > 0) // 在当前维护容器非空时查询代码
            {
                var firstChild = taskPaneDic.First();
                var taskPaneGroup = firstChild.Value;
                _anStormPaneWidth = taskPaneGroup.AnStormPane.Width;
                _anRecorderPaneWidth = taskPaneGroup.AnRecorderPane.Width;
                _anMacroPaneWidth = taskPaneGroup.AnMacroPane.Width;
                _anCommandPaneWidth = taskPaneGroup.AnCommandPane.Width;
            }
        }

        public static void SaveTaskPaneConfig()
        {
            var path = CoreManipulator.TaskPaneConfig;
            var xmldoc = new XmlDocument();
            var root = xmldoc.CreateElement("TaskPaneConfigRoot");
            var stormPaneWidth = xmldoc.CreateElementWithValue("StormPaneWidth", _anStormPaneWidth);
            var recorderPaneWidth = xmldoc.CreateElementWithValue("RecorderPaneWidth", _anRecorderPaneWidth);
            var macroPaneWidth = xmldoc.CreateElementWithValue("MacroPaneWidth", _anMacroPaneWidth);
            var commandPaneWidth = xmldoc.CreateElementWithValue("CommandPaneWidth", _anCommandPaneWidth);

            root.AppendChildren(stormPaneWidth, recorderPaneWidth, macroPaneWidth, commandPaneWidth);
            xmldoc.AppendChild(root);
            xmldoc.Save(path);
        }
    }
}
